/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package makao.server;

import java.util.ArrayList;
import shared.Card;
import shared.GameInfo;
import shared.MakaoPackage;
import shared.MyCards;
import shared.PlayerInfo;

/**
 *
 * @author latata_2
 */
public class SendPackageController {

    private Server server;
    private Timer timer;

    public SendPackageController(Server server) {
        this.server = server;
        this.timer = new Timer(this);
        this.timer.start();
    }

    public void sendStartGameInfo(ArrayList<Player> players, Player whoseTurn, Card topCard) {

        ArrayList<PlayerInfo> playersInfo = new ArrayList<PlayerInfo>();
        String firstPlayerName = whoseTurn.getPlayerName();
        for (Player player : players) {
            playersInfo.add(player.getPlayerInfo());
        }
        GameInfo gameInfo = new GameInfo(playersInfo, firstPlayerName, topCard);
        for (Player player : players) {
            player.getPlayerConnection().send(new MakaoPackage(MakaoPackage.GAME_INFO, gameInfo, new MyCards(player.getCards())));
        }



    }

    public void sendLoginConfirmation(PlayerConnection playerConnection) {
        ArrayList<String> list = new ArrayList<String>();
        list.add("OK");
        playerConnection.send(new MakaoPackage(MakaoPackage.LOGIN_RESPONSE, list));
    }

    public void sendLoginFailed(PlayerConnection playerConnection, String errorMsg) {
        ArrayList<String> list = new ArrayList<String>();
        list.add("FAILED");
        list.add(errorMsg);
        playerConnection.send(new MakaoPackage(MakaoPackage.LOGIN_RESPONSE, list));
    }

    public void sendGameInfo() {
        Game game = server.getGame();
        ArrayList<PlayerInfo> playersInfo = new ArrayList<PlayerInfo>();
        String whoseTurnPlayerName = game.getWhoseTurn().getPlayerName();
        for (Player player : game.getPlayers()) {
            playersInfo.add(player.getPlayerInfo());
        }
        GameInfo gameInfo = new GameInfo(playersInfo, whoseTurnPlayerName, game.getTopCard(), game.getDemandCardType(), game.getDemandCardSuit(), game.getCardsToTake(), game.getTurnsToWait());
        for (Player player : game.getPlayers()) {
            player.getPlayerConnection().send(new MakaoPackage(MakaoPackage.GAME_INFO, gameInfo, new MyCards(player.getCards())));
        }
        this.timer.zeruj();
    }

    void sendMessgae(PlayerConnection playerConnection, String string, String message) {
        ArrayList<String> list = new ArrayList<String>();
        list.add(message);
        playerConnection.send(new MakaoPackage(MakaoPackage.ERROR, list));
    }

    void sendMessgae2All(String string, String message) {
        for (Player player : server.getPlayers()) {
            ArrayList<String> list = new ArrayList<String>();
            list.add(message);
            player.getPlayerConnection().send(new MakaoPackage(MakaoPackage.GAME_FINISHED, list));
        }

    }

    public Server getServer() {
        return server;
    }
}
